package com.tnstudio.shaketoopenapps;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.ironsource.mobilcore.CallbackResponse;
import com.ironsource.mobilcore.MobileCore;
import com.ironsource.mobilcore.MobileCore.AD_UNITS;
import com.ironsource.mobilcore.MobileCore.LOG_TYPE;
import com.tnstudio.shaketoopenapps.R;
import com.tnstudio.shaketoopenapps.SettingData;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {

	ToggleButton tbShake;
	SeekBar sbHold;
	Button btnChoose;
	TextView tvMyApp;
	TextView tvValue;
	MySharedPreferences share;
	private InterstitialAd interstitial;
	private Activity mActivity;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		BindData();
		Thread.setDefaultUncaughtExceptionHandler(new ForceErrorCaughter(
				MainActivity.this));

		Advertise();
	}

	private void BindData() {
		tbShake = (ToggleButton) findViewById(R.id.tbShake);
		sbHold = (SeekBar) findViewById(R.id.sbHold);
		btnChoose = (Button) findViewById(R.id.btnChoose);
		tvMyApp = (TextView) findViewById(R.id.tvMyApp);
		tvValue = (TextView) findViewById(R.id.tvValue);
	
		// Event
		// --tbShake
		tbShake.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Intent mIntent = new Intent(MainActivity.this, MyService.class);
				if (tbShake.isChecked()) {
					// Neu nut dang la ON
					startService(mIntent);
					
				} else {
					// Neu nut dang la OFF
					stopService(mIntent);
				}
			}
		});
		// --sbHold
		sbHold.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				
				if (progress < 10)
					progress = 10;
				SettingData.threshold = progress;
				tvValue.setText(progress+"");
			}
		});
		// --btnChoose
		btnChoose.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ChooseApp();
			}
		});
		// --tvMyApp
		// --tvValue
		// SharedPreferences
	

	}

	// Event Function

	@Override
	public void onPause() {

		super.onPause();
		// goi ham lua trang thai
		share = new MySharedPreferences(this,tbShake.isChecked(),sbHold.getProgress(),"","");
		share.savingPreferences();
	}

	@Override
	public void onResume() {

		super.onResume();
		// goi ham trang thai
		share = new MySharedPreferences(this);
		share.restoringPreferences(tbShake,sbHold,tvMyApp,tvValue);
	}

	// End SharedPreferences
	private void ChooseApp() {
		Intent intent = new Intent(getApplicationContext(), AllAppsActivity.class);
		startActivity(intent);
	}

	// End Event Function
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	//Advertise
	private void Advertise() {
		
		// Create
		interstitial = new InterstitialAd(this);
		interstitial.setAdUnitId(AdvID.admobId);

		// Create ad request.
		AdRequest adRequest1 = new AdRequest.Builder().build();
		AdRequest adRequest2 = new AdRequest.Builder().build();

		//Load Banner
		AdView adView = (AdView)this.findViewById(R.id.adView); 
		adView.loadAd(adRequest1);
		
		// Begin loading Full Screen
		interstitial.loadAd(adRequest2);

		interstitial.setAdListener(new AdListener() {
			public void onAdLoaded() {
				displayInterstitial();
			};

			@Override
			public void onAdFailedToLoad(int errorCode) {
				MobileCore.showInterstitial(MainActivity.this, null);
				super.onAdFailedToLoad(errorCode);
			}
		});

		mActivity = this;
		MobileCore.init(this, AdvID.mobileCoreId, LOG_TYPE.DEBUG,
				AD_UNITS.ALL_UNITS);
		showStickeezWhenReady();
	}
	public void displayInterstitial() {
		if (interstitial.isLoaded()) {
			interstitial.show();
		}
	}

	private void showStickeezWhenReady() {
		if (MobileCore.isStickeeReady()) {
			MobileCore.showStickee(mActivity);
		}
	}

	@Override
	public void onBackPressed() {
		MobileCore.showInterstitial(MainActivity.this,
			new CallbackResponse() {
				@Override
				public void onConfirmation(TYPE type) {
					finish();
				}
			});
	}
	
	
}
